import FlowSetting from '@model/types/FlowSetting'
import TaskType from '@model/entity/TaskType'
import Field from '@model/entity/Field'
import { parseObject } from '@src/util/lang/object'
import { isObject } from '@src/util/type'

class FieldSimple {
  displayName: string = ''
  enable: number = 0
  id: string = ''
  name: string = ''
  setting: string = ''
  
  public static toField(fieldSimple: FieldSimple): Field {
    return {
      fieldId: fieldSimple?.id,
      tenantId: '',
      tableName: '',
      isSystem: undefined,
      fieldName: fieldSimple?.name,
      displayName: fieldSimple?.displayName,
      formType: '',
      defaultValue: null,
      isNull: undefined,
      isValidate: undefined,
      isSearch: undefined,
      isAdd: undefined,
      isDelete: undefined,
      isReadonly: undefined,
      placeHolder: null,
      orderId: null,
      tableChsName: undefined,
      isGuideData: undefined,
      guideProfessions: [],
      iid: undefined,
      id: fieldSimple?.id,
      setting: parseObject(fieldSimple?.setting),
      enabled: fieldSimple?.enable,
      field: fieldSimple?.name
    }
  }
}

class TaskTypeSimple {
  id: string = ''
  /* 工单类型 颜色  sample: rgb(115,127,124)  */
  color?: string
  //卡片设置
  cardSetting?: Record<string, any>
  // 配置完之前是禁用状态 配置完后点下发布则启用 初始化默认类型时set为1
  enabled?: number 
  // 字段列表 移动端查询所有工单类型会返回
  field?: Record<string, FieldSimple>
  // 流程设置
  flowSetting?: {
    create: FlowSetting,
    allot: FlowSetting,
    accept: FlowSetting,
    start: FlowSetting,
    finish: FlowSetting,
    cost: FlowSetting,
    review: FlowSetting,
    autoReview: FlowSetting,
    close: FlowSetting,
    off: FlowSetting,
  }
  isDelete?: number
  name?: string
  // 优先级
  orderId?: number
  templateId: string = ''
  
  public static toTaskType(taskTypeSimple: TaskTypeSimple): TaskType {
    const FieldSimpleObject = isObject(taskTypeSimple?.field) ? taskTypeSimple.field : {}
    let fields: Field[] = []
    
    for (let key in FieldSimpleObject) {
      fields.push(
        FieldSimple.toField(
          FieldSimpleObject[key]
        )
      )
    }
    
    return {
      defaultColors: [],
      id: taskTypeSimple?.id ?? taskTypeSimple.templateId,
      name: taskTypeSimple?.name,
      enabled: taskTypeSimple?.enabled,
      flowSetting: taskTypeSimple?.flowSetting,
      createTime: undefined,
      isDelete: taskTypeSimple?.isDelete,
      cardSetting: taskTypeSimple?.cardSetting,
      overTimeSetting: {},
      allowPause: undefined,
      pauseApprovers: [],
      options: {},
      reportSetting: {},
      printSetting: {},
      planRemindSetting: {},
      delayBack: undefined,
      delayBackMin: undefined,
      isLeader: undefined,
      noticeLeader: undefined,
      noticeUsers: [],
      tags: [],
      labels: [],
      professions: [],
      description: undefined,
      orderId: taskTypeSimple?.orderId,
      guideProfessions: [],
      isGuideData: undefined,
      config: undefined,
      field: fields,
      templateId: taskTypeSimple?.templateId 
    }
  }
  
}

export default TaskTypeSimple